import { constantRoutes, asyncRoutes } from '@/router/index'
// 全局处理权限筛选
const state = {
  routes: []
}

const mutations = {
  setRoutes(state, asyncRoutes) {
    // 合并静态路由和动态路由
    const newRoutes = [...constantRoutes, ...asyncRoutes]
    state.routes = newRoutes
  }
}

const actions = {
// 封装一个筛选可用路由的方法
  filterRoutes(store, menus) {
    const routes = asyncRoutes.filter(route => menus.indexOf(route.name) > -1)
    // console.log('筛选')
    // console.log(routes)
    // 解决菜单问题, 将筛选过后的动态路由交给 mutations
    store.commit('setRoutes', routes)
    return routes
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
